//
//  SingerMusicListTableViewCell.m
//  YanMusicPlayer
//
//  Created by 1564335 on 15-6-12.
//  Copyright (c) 2015年 胡岩 All rights reserved.
//

#import "SingerMusicListTableViewCell.h"
#import "SongListViewController.h"

@implementation SingerMusicListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addToList:)];
//        _longPress.minimumPressDuration = 1.0;
//        [self addGestureRecognizer:_longPress];
        self.singerName = [[UILabel alloc] init];
        [self.contentView addSubview:self.singerName];
        self.musicName = [[UILabel alloc] init];
        [self.contentView addSubview:self.musicName];
        self.collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.collectBtn];
        self.downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.downloadBtn];
    }
    return self;
}

- (void)addToList:(UIButton *)longPress
{
    NSArray *array = [Database selectTableName];
    if (array.count > 0) {
        _isGetAlert = YES;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"添加到:" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        alert.tag = 1;
        for (NameModel *model in array) {
            [alert addButtonWithTitle:model.name];
        }
        [alert show];
    } else {
        _isGetAlert = YES;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前无列表,请新建列表" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        alert.tag = 2;
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1 && buttonIndex == 0) {
        return;
    } else if (alertView.tag == 2 && buttonIndex == 1) {
        return;
    } else if (alertView.tag == 3 && buttonIndex == 1) {
        return;
    }
    BOOL isHaveSong = NO;
    if (alertView.tag != 2 && buttonIndex != 0) {
        NSArray *tempArray = [[MusicPlayer1 shareInstance].mainList objectAtIndex:buttonIndex - 1];
        for (MusicModel *tempModel in tempArray) {
            if ([_singerMusicListModel.song_id integerValue] == [tempModel.songId integerValue]) {
                isHaveSong = YES;
                break;
            }
        }
    }
    // 修复歌曲重复添加
    if (isHaveSong) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已经添加" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 5;
        [alert show];
        return;
    }
    if (alertView.tag == 1) {
        if (buttonIndex == 0) {
            return;
        }
        NSMutableArray *array = [[MusicPlayer1 shareInstance].mainList objectAtIndex:buttonIndex - 1];
        MusicModel *musicModel = [MusicModel getMusicModel];
        musicModel.url = _singerMusicListModel.url;
        musicModel.singerName = _singerMusicListModel.singer_name;
        musicModel.songName = _singerMusicListModel.song_name;
        musicModel.lrcId = _singerMusicListModel.song_id;
        musicModel.singerId = _singerMusicListModel.singer_id;
        musicModel.songId = _singerMusicListModel.song_id;
        musicModel.musicLength = _singerMusicListModel.duration;
        [array addObject:musicModel];
        [[MusicPlayer1 shareInstance].mainList replaceObjectAtIndex:buttonIndex - 1 withObject:array];
        if ([HearderView shareInstance].currentList - 2 == buttonIndex - 1) {
            [[MusicPlayer1 shareInstance].list removeAllObjects];
            [[MusicPlayer1 shareInstance].list addObjectsFromArray:array];
            [[HearderView shareInstance].listTableView reloadData];
        }
        NSArray *nameArray = [Database selectTableName];
        NameModel *nameModel = [nameArray objectAtIndex:buttonIndex - 1];
        [Database insertCustomValue:musicModel :nameModel.tableName];
    } else if (alertView.tag == 2) {
        if (buttonIndex == 1) {
            return;
        }
        [[CDSideBarController sharedInstance] dismissMenu];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入歌单名称" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        alert.tag = 3;
        [alert show];
    }  else if (alertView.tag == 3) {
        if (buttonIndex == 1) {
            return;
        }
        NSString *text = [alertView textFieldAtIndex:0].text;
        if (buttonIndex == 0 && text.length != 0) {
            NSInteger str = [[_USERDEFAULT objectForKey:@"tableCount"] integerValue];
            [Database insertTableName:[NSString stringWithFormat:@"%ld", str] :text];
            NSString *getStr = [NSString stringWithFormat:@"%ld", ++str];
            [_USERDEFAULT setObject:getStr forKey:@"tableCount"];
            NSMutableArray *array = [NSMutableArray array];
            [[MusicPlayer1 shareInstance].mainList addObject:array];
            [Database createTable];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"列表添加成功请长按添加歌曲" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//            alert.tag = 4;
//            [alert show];
        }
        
    }
}


- (void)setSingerMusicListModel:(SingerMusicListModel *)singerMusicListModel
{
    _singerMusicListModel = singerMusicListModel;
    if (_singerMusicListModel.singer_name != nil && ![_singerMusicListModel.singer_name isEqual:[NSNull null]]) {
        _singerName.text = _singerMusicListModel.singer_name;
        _singerName.font = [UIFont systemFontOfSize:11];
    }
     _musicName.text = _singerMusicListModel.song_name;
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    _collectBtn.frame = CGRectMake(10, 5, self.frame.size.height - 10, self.frame.size.height - 10);
//    _collectBtn.backgroundColor = [UIColor yellowColor];
    _musicName.frame = CGRectMake(70, 10, self.frame.size.width - self.frame.size.height - 70, (self.frame.size.height - 30) / 3 * 2);
    _singerName.frame = CGRectMake(70, _musicName.frame.size.height + 20, self.frame.size.width - self.frame.size.height - 70, (self.frame.size.height - 30) / 3 + 5);
    _downloadBtn.frame = CGRectMake(self.frame.size.width - self.frame.size.height, 9, self.frame.size.height - 18, self.frame.size.height - 18);
//    _downloadBtn.backgroundColor = [UIColor yellowColor];
    
    _collectBtn.showsTouchWhenHighlighted = YES;
    _downloadBtn.showsTouchWhenHighlighted = YES;
    
    _downloadBtn.alpha = 0.6;
    [_collectBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", _collectImage]]forState:UIControlStateNormal];
    [_downloadBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", @"iconfont-tianjia"]] forState:UIControlStateNormal];
    [_downloadBtn addTarget:self action:@selector(addToList:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
